<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/static/blog/bs/css/bootstrap.css">
</head>
<body>
<h3>登录</h3>

<div class="container">
    <div class="row">
        <div class="col-md-6 col-lg-offset-3">
            <form>
                {% csrf_token %}
                <div class="form-group">
                    <label for="user">用户名</label>
                    <input type="text" id="user" class="form-control">
                </div>
                <div class="form-group">
                    <label for="pwd">密码</label>
                    <input type="password" id="pwd" class="form-control">
                </div>

                <div class="form-group">
                    <label for="pwd">验证码</label>
                    <div class="row">
                        <div class="col-md-6">
                            <input type="text" id="valid_code" class="form-control">
                        </div>
                        <div class="col-md-6">
                            <img height="40" width="270" id="valid_code_img" src="/get_validCode_img/" alt="">
                        </div>
                    </div>
                </div>

                <input type="button" class="btn btn-default login_btn" value="登录"><span class="error"></span>
                <a href="/register/" class="btn btn-success pull-right">注册</a>
                <div class="form-group">
                    <a href="/index/" class="btn btn-success pull-right form-control" style="margin-top: 30px">不登录，进入主页</a>
                </div>
            </form>
        </div>
    </div>
</div>

<script src="/static/js/jquery-3.6.0.min.js"></script>
<script>
    $("#valid_code_img").click(function () {
        // 刷心验证码
        $(this)[0].src+="?"
    })

    // 登录验证
    $(".login_btn").click(function () {
        $.ajax({
            url:"",
            type:"post",
            data:{
                user:$("#user").val(),
                pwd:$("#pwd").val(),
                valid_code:$("#valid_code").val(),
                csrfmiddlewaretoken:$("[name='csrfmiddlewaretoken']").val(),
            },
            success:function (login_data) {
                console.log(login_data)
                if (login_data.user){
                    location.href="/index/"
                }
                else {
                    $(".error").text(login_data.msg).css({ "color":"red", "margin-left":"20px" })
                    setTimeout(function () {
                        $(".error").text(login_data.msg).css({ "color":"red", "margin-left":"20px" }).text("")

                    }, 1000)
                }
            }
        })
    })
</script>

</body>
</html>